//
//  MPlusUserViewController.m
//  medicineplus
//
//  Created by gufei on 15-5-14.
//  Copyright (c) 2015年 chn_ruby@126.cn. All rights reserved.
//

#import "MPlusUserViewController.h"
#import "MPlusUserTableViewCell.h"
#import "MPlusMyCollectionViewController.h"
#import "MPlusMyMessageListViewController.h"
#import "MPlusString.h"
#import "MPlusDefine.h"

@interface MPlusUserViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *aMenuIconListArray;
@property (nonatomic, strong) NSMutableArray *aMenuLabelListArray;
@property (nonatomic, retain) UITableView *aTableView;

@end

@implementation MPlusUserViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        [self setTabBarResource:kTabBarTitleUser setImage:kTabBarImageUser setSelectedImage:kTabBarSelectedImageUser];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _aMenuIconListArray = [@[kMenuIconOrder,
                             kMenuIconCollection,
                             kMenuIconAddress,
                             kMenuIconMessage,
                             kMenuIconCall,
                             kMenuIconSetting
                             ] mutableCopy];
    _aMenuLabelListArray = [@[kLabelMyOrder,
                             kLabelMyCollection,
                             kLabelHarvestAddress,
                             kLabelMessage,
                             kLabelCallCenter,
                             kLabelSetting
                             ] mutableCopy];
    // 初始化头部视图
    [self initHeadView];
    // 初始化菜单
    [self initMenuList];
}

- (void)initHeadView {
    UIImageView *headBg = [[UIImageView alloc] init];
    headBg.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.4);
    [headBg setImage:[UIImage imageNamed:@"bg_usercenter.png"]];
    [self.view addSubview:headBg];
    
    UIButton *shopCartIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    shopCartIcon.frame = CGRectMake((SCREEN_WIDTH-PADDING_20-30), 30, 30, 27);
    [shopCartIcon setBackgroundImage:[UIImage imageNamed:@"ic_shopcart_s.png"] forState:UIControlStateNormal];
    [self.view addSubview:shopCartIcon];
    
    UIButton *headIcon = [UIButton buttonWithType:UIButtonTypeCustom];
    headIcon.frame = CGRectMake(SCREEN_WIDTH/2-50, 70, 100, 100);
    [headIcon setBackgroundImage:[UIImage imageNamed:@"ic_user.png"] forState:UIControlStateNormal];
    [self.view addSubview:headIcon];
    
    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-40, 190, 80, LABEL_HEIGHT_20)];
    name.font = [UIFont systemFontOfSize:[kFontSize16 floatValue]];
    name.textColor = [UIColor whiteColor];
    name.textAlignment = NSTextAlignmentCenter;
    name.text = @"赵大牛";
    [self.view addSubview:name];
    
    UILabel *position = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-40, 220, 80, LABEL_HEIGHT_20)];
    position.font = [UIFont systemFontOfSize:[kFontSize16 floatValue]];
    position.textColor = [UIColor whiteColor];
    position.textAlignment = NSTextAlignmentCenter;
    position.text = @"主任";
    [self.view addSubview:position];
    
    UILabel *hospital = [[UILabel alloc] initWithFrame:CGRectMake(PADDING_10, 220, (SCREEN_WIDTH-80-PADDING_20)/2, LABEL_HEIGHT_20)];
    hospital.font = [UIFont systemFontOfSize:[kFontSize16 floatValue]];
    hospital.textColor = [UIColor whiteColor];
    hospital.textAlignment = NSTextAlignmentLeft;
    hospital.text = @"华佗医院";
    [self.view addSubview:hospital];
    
    UILabel *specialize = [[UILabel alloc] initWithFrame:CGRectMake(PADDING_10+(SCREEN_WIDTH-80-PADDING_20)/2+80, 220, (SCREEN_WIDTH-80-PADDING_20)/2, LABEL_HEIGHT_20)];
    specialize.font = [UIFont systemFontOfSize:[kFontSize16 floatValue]];
    specialize.textColor = [UIColor whiteColor];
    specialize.textAlignment = NSTextAlignmentRight;
    specialize.text = @"口腔专业";
    [self.view addSubview:specialize];
}

- (void)initMenuList {
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT*0.4, SCREEN_WIDTH, SCREEN_HEIGHT*0.6-TABBAR_HEIGHT) style:UITableViewStylePlain];
    // 设置tableView的数据源
    tableView.dataSource = self;
    // 设置tableView的委托
    tableView.delegate = self;
    self.aTableView = tableView;
    [self.view addSubview:self.aTableView];
    // 去除多余的分割线
    [self setExtraCellLineHidden:_aTableView];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return _aMenuIconListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    NSString *cellIdentifier = [NSString stringWithFormat:@"%lu", (unsigned long)row];
    MPlusUserTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[MPlusUserTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                                reuseIdentifier:cellIdentifier];
    }
    cell.aMenuIcon.image = [UIImage imageNamed:_aMenuIconListArray[row]];
    cell.aTitle.text = _aMenuLabelListArray[row];
    if (row == 4) {
        cell.aIntro.text = @"400-000-0000";
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSUInteger row = [indexPath row];
    switch (row) {
        case 0:
            // 我的订单
           
            break;
        case 1: {
            // 我的收藏
            self.navigationController.navigationBarHidden = NO;
            MPlusMyCollectionViewController *collectionVC = [[MPlusMyCollectionViewController alloc] initWithNibName:@"MPlusMyCollectionViewController" bundle:nil];
            [self.navigationController pushViewController:collectionVC animated:YES];
            }
            break;
        case 2:
            // 收货地址
            break;
        case 3: {
            // 消息
            self.navigationController.navigationBarHidden = NO;
            MPlusMyMessageListViewController *messageVC = [[MPlusMyMessageListViewController alloc] initWithNibName:nil bundle:nil];
            [self.navigationController pushViewController:messageVC animated:YES];
            }
            break;
        case 4:
            // 客服电话
            [self callPhone:@"400-000-0000"];
            break;
        case 5:
            // 设置
            break;
        default:
            break;
    }
    //消除cell选择痕迹
    [self performSelector:@selector(deselect) withObject:nil afterDelay:0.5f];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma mark -
#pragma mark Table View setExtraCellLineHidden Methods
- (void)setExtraCellLineHidden: (UITableView *)tableView {
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}

#pragma mark -
#pragma mark Table View Cell deselect Methods
- (void) deselect {
    [_aTableView deselectRowAtIndexPath:[_aTableView indexPathForSelectedRow] animated:YES];
}

-(void)callPhone:(NSString *)phoneNum {
    NSURL *phoneURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@",phoneNum]];
    UIWebView *phoneCallWebView = [[UIWebView alloc] initWithFrame:CGRectZero];// 这个webView只是一个后台的容易 不需要add到页面上来  效果跟方法二一样 但是这个方法是合法的
    [phoneCallWebView loadRequest:[NSURLRequest requestWithURL:phoneURL]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
